package com.nwpu.coursesupport.service.imple;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.AssistantSearchCriteria;
import com.nwpu.coursesupport.entity.Assistant;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.mapper.AssistantMapper;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.RegisterParam;
import com.nwpu.coursesupport.tool.EncryptionUtil;
import com.nwpu.coursesupport.tool.StringUtil;
import com.nwpu.coursesupport.tool.UpdateUtil;
/**
 * @author archerkang
 */
@Service
public class AssistantService extends UserService{
	
    @Autowired
    private AssistantMapper assistantMapper;

    protected static Logger logger = LoggerFactory.getLogger(AssistantService.class);
    
    public JSONResult detail(IDParam p){
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        Assistant assistant = this.assistantMapper.findById(p.getId());
        if(assistant==null){
        	logger.error("该用户不存在");
        	return rst.fail("该用户不存在");
        }
        RegisterParam dao=new RegisterParam();
        UpdateUtil.setValues(dao, assistant);
        dao.setUserCode(assistant.getAssistantCode());
        rst.addData("user", dao);
        rst.success();
        return rst;
    }


    public JSONResult create(RegisterParam p){
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        AssistantSearchCriteria sc=new AssistantSearchCriteria();
        sc.setAssistantCode(p.getUserCode());
        List<Assistant> list=this.assistantMapper.find(sc);
        if(list!=null&&list.size()>0){
        	logger.error("该用户已存在");
        	return rst.fail("该用户已存在");
        }
        
        Assistant assistant = new Assistant();
        UpdateUtil.setValues(assistant, p);
		assistant.setAssistantCode(p.getUserCode());
		
		assistant.setPassword(EncryptionUtil.Encryption(assistant.getPassword(),assistant.getAssistantCode()));
		if(StringUtil.isEmpty(assistant.getPicture())){
			assistant.setPicture(getHeaderImage(assistant.getGender()));
        }
		Date date=new Date();
		assistant.setCreateDate(date);
		assistant.setUpdateDate(date);
        if (!this.assistantMapper.save(assistant)){
        	logger.error("保存失败");
            return rst.fail("保存失败");
        }
        rst.addData("assistant", assistant);
        rst.success();
        return rst;
    }

    public JSONResult updateAvatar(RegisterParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null ||p.getPicture()==null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        
        Integer id=(Integer) getSession().getAttribute("id");
        Assistant assistant = this.assistantMapper.findById(id);
        if (assistant == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }
        assistant.setPicture(p.getPicture());
        if(!this.assistantMapper.update(assistant)){
        	logger.error("用户修改失败");
        	return rst.fail("用户修改失败");
        }
        getSession().setAttribute("image",assistant.getPicture());
        return rst.success();
    }
    
    public JSONResult update(RegisterParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null ){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        
        Integer id=(Integer) getSession().getAttribute("id");
        p.setId(id);
        Assistant assistant = this.assistantMapper.findById(p.getId());
        if (assistant == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }
        
        if (UpdateUtil.setValues(assistant, p) > 0){
        	assistant.setUpdateDate(new Date());
            if (!this.assistantMapper.update(assistant)){
            	logger.error("保存失败");
                return rst.fail("保存失败");
            }
        }
        
        RegisterParam dao=new RegisterParam();
        UpdateUtil.setValues(dao, assistant);
        dao.setUserCode(assistant.getAssistantCode());
        rst.addData("user", dao);
        rst.success();
        return rst;
    }
    
    public JSONResult listData(AssistantSearchCriteria sc) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
       
        if (sc == null || StringUtil.isEmpty(sc.getKeywords())){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        
        List<Assistant> list = this.assistantMapper.find(sc);
        if (list == null){
        	logger.error("数据查询失败");
            return rst.fail("数据查询失败");
        }
        
        rst.addData("list", list);
        rst.addData("search", sc);
        rst.success();
        return rst;
    }
    
    public Assistant findByAssistantCode(String assistantCode){
    	Assistant assistant = this.assistantMapper.findByAssistantCode(assistantCode);
    	return assistant;
    }
}
